package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.pojo.SysOperLog;
import com.cl.short_video_java.service.SysOperLogService;
import com.cl.short_video_java.mapper.SysOperLogMapper;
import com.cl.short_video_java.util.DateUtils;
import com.cl.short_video_java.util.ExceUtil;
import com.cl.short_video_java.vo.sys.OperLogExceSearch;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author object
 * @description 针对表【sys_oper_log(操作日志记录)】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
		implements SysOperLogService {

	@Override
	public Boolean deleteService(String idlist) {
		String[] split = idlist.split(",");
		boolean removeBatchByIds = this.removeBatchByIds(Arrays.stream(split).toList());
		return removeBatchByIds;
	}

	@Override
	public Boolean clearOperLogService() {
		boolean remove = this.remove(null);
		return remove;
	}

    @Override
    public PageInfo<SysOperLog> pageSearchService(OperLogExceSearch operLogExceSearch) {
        if(operLogExceSearch.getPageNum()==null || operLogExceSearch.getPageSize()==null){
            operLogExceSearch.setPageNum(1);
            operLogExceSearch.setPageSize(20);
        }
        PageHelper.startPage(operLogExceSearch.getPageNum(), operLogExceSearch.getPageSize());
        List<SysOperLog> list = this.list(new QueryWrapper<SysOperLog>()
                .eq(!operLogExceSearch.getOperIp().equals(""), "oper_id", operLogExceSearch.getOperIp())
                .eq(!operLogExceSearch.getOperName().equals(""), "oper_name", operLogExceSearch.getOperName())
                .eq(!operLogExceSearch.getTitle().equals(""), "title", operLogExceSearch.getTitle())
                .eq(operLogExceSearch.getBusinessType() != null, "business_type", operLogExceSearch.getBusinessType())
                .eq(operLogExceSearch.getStatus() != null, "status", operLogExceSearch.getStatus())
				.orderByDesc("oper_id"));
        PageInfo<SysOperLog> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


	@Override
	public ResponseEntity<byte[]> getExceService(OperLogExceSearch operLogExceSearch) throws IOException {
		List<SysOperLog> list = new ArrayList<>();
		String formt = DateUtils.YYYY_MM_DD;
		if (operLogExceSearch.getIdlist().length() > 0) {
			String[] split = operLogExceSearch.getIdlist().split(",");
			list = this.listByIds(Arrays.stream(split).toList());
		} else {
			list = this.list(new QueryWrapper<SysOperLog>()
					.eq(!operLogExceSearch.getOperIp().equals(""), "oper_ip", operLogExceSearch.getOperIp())
					.eq(!operLogExceSearch.getOperName().equals(""), "oper_name", operLogExceSearch.getOperName())
					.eq(!operLogExceSearch.getTitle().equals(""), "title", operLogExceSearch.getTitle())
					.eq(operLogExceSearch.getBusinessType() != null, "business_type", operLogExceSearch.getBusinessType())
					.eq(operLogExceSearch.getStatus() != null, "status", operLogExceSearch.getStatus()));
		}
		ByteArrayOutputStream outputStream = ExceUtil.exportSysOperLogExcel(list);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("sysoperlog", "操作日志.xlsx");
		return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK);
	}

	@Override
	public Page<SysOperLog> homePage(Integer num, Integer size) {
		Page<SysOperLog> page = this.page(new Page<>(num, size), new QueryWrapper<SysOperLog>()
				.select("title", "business_type", "status", "request_method", "oper_name", "oper_url")
				.orderByDesc("oper_time"));
		return page;
	}
}




